// pages/con_order/con_order.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hostUrl: app.globalData.hostUrl,
    note: '',
    count: '',
    pay_method: '2',
    button: 'true',
    pay: [
      { name: '2', value: '微信支付', img: '../images/wechat.png', checked: 'true' },
      { name: '4', value: '蓝旗币', img: '../images/wechat.png' },
      { name: '3', value: '可提现', img: '../images/wechat.png' },
      // { name: '2', value: '钱包支付', img: '../images/积分.png' },
      // { name: '3', value: '佣金支付', img: '../images/报单.png' },

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    that.setData({
      id: options.id,
      count: options.count,
    })
    // if(that.data.daili==1){
    //   var pay=that.data.pay;
    //   pay.splice(pay,1)
    //   console.log(pay,'删除支付方式')
    //   that.setData({
    //     pay:pay,
    //     pay_method:'',
    //   })
    // }
    // console.log(that.data.type,'购买类型')

    that.getuser();
    // console.log(that.data.Category,'级别id')
    var userId = wx.getStorageSync('userId');
    if (userId != null && userId > 0 && userId != '') {
      that.buy();//读取购物车
      // that.dTotal();//读取金额
      // that.getAddressList();
      // that.memberlelev();
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '请先登录后购买',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/mine/mine',
            })
          } else if (res.cancel) {
          }
        }
      })
    }

  },
  //支付方式选择
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      pay_method: e.detail.value,
    })
    console.log(this.data.pay_method, '支付方式')
  },
  //获取用户级别信息
  // memberlelev:function(){
  //   var that=this;

  //   wx.request({
  //     url: app.globalData.apiUrl + 'User/getUserlevel',
  //     data: {
  //       // opt: 'getCartList',
  //       userlelevid: that.data.userlelevid,
  //     },
  //     success: function (res) {
  //       if (res.data != null) {
  //         that.setData({
  //           userlelevinfo: res.data,
  //         })
  //         console.log(that.data.dTotal,'原金额')
  //         console.log(that.data.dTotal * (that.data.userlelevinfo.xiaofeiyouhui / 100, '优惠金额'))
  //         that.setData({
  //           dTotal: that.data.dTotal - (that.data.dTotal * (that.data.userlelevinfo.xiaofeiyouhui / 100))
  //         })
  //       }
  //     }
  //   })
  // },
  //获取用户积分
  getuser: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'User/getUser',
      data: {
        // opt: 'getCartList',
        userId: wx.getStorageSync('userId'),
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            userscores: res.data.Scores,//蓝旗比
            usercommission: res.data.Commission,//可提现
          })
        }
      }
    })
  },
  buy: function () {//读取购物车
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/buy',
      data: {
        userId: wx.getStorageSync('userId'),
        id:that.data.id,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            info:res.data,
            zong_dTotal:res.data.card.Price
          })
        }
        console.log(that.data.info,'详情')
      }
    })
  },
  dTotal: function () {//读取金额
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/dTotal',
      data: {
        // opt: 'dTotal',
        userId: wx.getStorageSync('userId'),
        key: that.data.key,
        count: that.data.count,
        productid: that.data.productid,
        type: that.data.type,
        Category: that.data.Category,
        levelsid: wx.getStorageSync('userlelevid'),

      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            dTotal: res.data.dTotal,
            // marketPriceTotal: res.data.marketPriceTotal,
            quantity: res.data.quantity,
          })
        }
      }
    })
  },
  //读取默认地址
  getAddressList: function () {//读取收货地址
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/getAddressList',
      data: {
        // opt: 'getAddressList',
        userId: wx.getStorageSync('userId'),
        // where:' and is_default=1',
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        if (res.data != "") {
          that.setData({
            xuanzhong_address: res.data,

          })
        }
      }
    })
  },
  //选择地址后读取地址信息
  getxuanAddressList: function (addrid) {//读取收货地址
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/get_xcx_yunfei',
      data: {
        // opt: 'getAddressList',
        addId: addrid,
        type: that.data.type,
        product_id: that.data.productid,
        Key: that.data.key,
        num: that.data.zongcount
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res,'地址信息')
        if (res.data != null) {
          if (res.data.status == 0) {
            wx.showToast({
              title: '此地区不发货',
              icon: 'success',
              duration: 2000
            })
            return;
          }
          that.setData({
            xuanzhong_address: res.data.address,
            yunfei: res.data.freight,
            zong_dTotal: Number(that.data.dTotal) + Number(res.data.freight)
          })
        }
      }
    })
  },
  formSubmit: function (e) {//提交
    // console.log(e)
    // return ;
    var that = this;
    if (that.data.pay_method == "") {

      wx.showToast({
        title: '请选择支付方式',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    if (that.data.pay_method == 4) {
      console.log(that.data.zong_dTotal, '蓝旗币计算')
      if (that.data.zong_dTotal > that.data.userscores) {
        wx.showToast({
          title: '蓝旗币不足!',
          icon: 'none',
          duration: 1500
        })
        return;
      }
    }
    if (that.data.pay_method == 3) {
      // console.log(111,'积分判断')
      if (that.data.zong_dTotal > that.data.usercommission) {
        wx.showToast({
          title: '可提现不足!',
          icon: 'none',
          duration: 1500
        })
        return;
      }
    }
   
      that.setData({
        button: 'false',
      })
      wx.request({
        url: app.globalData.apiUrl + 'Hotel/createOrder', //接口地址
        data: {
          // opt: 'addOrder',
          userId: wx.getStorageSync('userId'),
          card_id: that.data.id,
          num: that.data.count,
          payway: that.data.pay_method,
          // Category: that.data.Category,
          // levelsid: wx.getStorageSync('userlelevid'),
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
      
          if (res.data.status > 0) {
            if (res.data.PayWay == 2) {
              that.pay(res.data.orderid, res.data.PayWay);

            } else {
              wx.redirectTo({
                url: '../new_inter/zhifu/zhifu?orderid=' + res.data.orderid + '&type=',
              })
            }
            // wx.showToast({
            //   title: '提交成功!',//这里打印出登录成功
            //   icon: 'success',
            //   duration: 1000,
            //   success:function(){
            //     wx.navigateTo({
            //       url: '/pages/personal/dingdan/dingdan',
            //     })
            //   }
            // })
          } else {
            wx.showToast({
              title: '提交失败!',
              icon: 'loading',
              duration: 1500
            })
          }
        }
      })
    
  },
  pay: function (id, payway) {//支付
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Pay/lvju_GetUnifiedOrderResult',
      data: {
        // opt: 'GetUnifiedOrderResult',
        userId: wx.getStorageSync('userId'),
        id: id,//订单id
        // typeId: 2,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res, '支付返回信息')
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            console.log(res)
            wx.navigateTo({
              url: '../personal/lvjuka/lvjuka',
            })
          },
          'fail': function (res) {
            wx.navigateTo({
              url: '../personal/lvjuka/lvjuka',
            })
          },
          'complete': function (res) {
            wx.navigateTo({
              url: '../personal/lvjuka/lvjuka',
            })
          }
        })

      }
    })
  },
  shippingMethod: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    if (id == 1) {
      that.setData({
        shippingMethodActive: 1,
        shipping_method: '门店自提',
        dTotal: that.data.dTotal - that.data.yunfei,//减运费
      }, function () {
      })
    } else if (id == 2) {
      that.setData({
        shippingMethodActive: 2,
        shipping_method: '邮寄到家',
        dTotal: that.data.dTotal + that.data.yunfei,//加运费
      }, function () {
      })
    }
  },
  //订单页面点击进入地址列表
  addAddress: function () {
    wx.navigateTo({
      url: '/pages/personal/address/address?isorderpage=1',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})